package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("goods")
public class SearchController {

    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @GetMapping("initialization")
    public Mono<String> initData(){
        // 创建索引库
        searchService.createIndexAndMapping();
        // 加载数据
         searchService.loadData();

        return Mono.just("数据导入成功！");
    }

    /**
     * 自动补全
     * @param keyPrefix 用户输入的内容
     * @return 补全的词条列表
     */
    @GetMapping("suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String keyPrefix){
        return searchService.getSuggestion(keyPrefix);
    }

    /**
     * 搜索商品
     * @param params 搜索条件
     * @return 商品列表和分页信息
     */
    @PostMapping("list")
    public Mono<PageInfo<Goods>> search(@RequestBody SearchParamDTO params){
        return searchService.search(params);
    }

    /**
     * 查询过滤项
     * @param params 查询条件
     * @return 过滤项
     */
    @PostMapping("filter")
    public Mono<Map<String,List<?>>> getFilters(@RequestBody SearchParamDTO params){
        return searchService.getFilters(params);
    }
}
